package com.kevin.common.util.tree;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 树形构造Util
 *
 * @author wh
 * @date 2021-06-07
 */
public class TreeUtil {
    public static  <T extends TreeNode> List<T> build(List<T> list, Object defaultParentValue) {
        list.forEach(e->{
            if(!e.getParentId().equals(defaultParentValue)){
                Optional<T> first = list.stream().filter(x -> x.getId().equals(e.getParentId())).findFirst();
                first.ifPresent(t -> t.getChildren().add(e));
            }
        });
        return list.stream().filter(e->e.getParentId().equals(defaultParentValue)).collect(Collectors.toList());
    }

}
